import React, { Component } from 'react';

import './index.less';

import {
    base,
    promptBox
} from 'nc-lightapp-front';

import ajaxAddTemplate from '../../request-functions/getAddTemplate';
import verifyOnlyInfo from '../../request-functions/verifyOnlyInfo';

import language from '../../language';

const {
    NCModal,
    NCButton,
    NCPopconfirm
} = base;

const {
    Header: NCModalHeader,
    Body: NCModalBody,
    Footer: NCModalFooter
} = NCModal;

const onlyInfoTemplate = 'only_info_template';


class OnlyInfoModal extends Component {
    constructor(props) {
        super(props);

        this.showHandle = this.showHandle.bind(this);
        this.closeInfoModal = this.closeInfoModal.bind(this);
        this.verifyOnlyInfo = this.verifyOnlyInfo.bind(this);
        this.verifyOnlyInfoStep2 = this.verifyOnlyInfoStep2.bind(this);
        this.getTemplate = this.getTemplate.bind(this);
    }

    async getTemplate() {
        const {
            humanResourceOrganizationValue: {
                refpk
            },
            meta: {
                getMeta,
                setMeta
            },
            updateState,
            form: {
                setFormStatus,
                setFormItemsValue
            }
        } = this.props;

        let postdata = {
            pk_org: refpk
        };

        try {
            let template = getMeta();
            // 证件类型默认值是按照 组织可以设置不同的
            // 每次必须调接口
            
            let res = await ajaxAddTemplate(postdata);
            if(!template['only_info_template']) {
                res.data.templateInfo.items.map((item) => {
                    item.attrcode = item.code || item.attrcode;
                });
        
                updateState({
                    uniqueFields: res.data.uniqueFields
                });
        
                template['only_info_template'] = res.data.templateInfo;
                // 让证件类型改成单选
                template['only_info_template'].items.map((item) => {
                    if(item.attrcode === 'idtype') {
                        item.isMultiSelectedEnabled = false;
                    }
                });
                setMeta(template, () => {
                }); 
            }
            if (res.data.idtype) {
                setFormItemsValue(onlyInfoTemplate, {'idtype': res.data.idtype})
            }
            setFormStatus(onlyInfoTemplate, 'add');
        }
        catch(e) {
        }
    }

    async verifyOnlyInfoStep2() {
        let values = this.props.form.getAllFormValue(onlyInfoTemplate);
        const {
            currentSelectDepartment,
            humanResourceOrganizationValue,
            uniqueFields
        } = this.props;

        let postData = {
            model: values,
            pk_org: humanResourceOrganizationValue.refpk,
            pk_psncl: values.rows[0].values['hi_psnjob_pk_psncl'].value,
            tree_node_id: currentSelectDepartment,
            uniqueFields: uniqueFields
        };
    
        try {
            let res = await verifyOnlyInfo(postData, 2);
            console.log('res', res);
            debugger
            if(res.success) {
                if(res.data.confirmMsg) {
                    promptBox({
                        color: 'warning',
                        title: '',
                        content: (
                            <div 
                                dangerouslySetInnerHTML={{
                                    __html: res.data.confirmMsg
                                }}
                            ></div>
                        ),
                        beSureBtnClick: () => {
                            this.props.updateState({
                                modifyData: res
                            }, () => {
                                this.props.processEditPageTemplate(() => {
                                    this.props.updateState({
                                        page: 'add-new',
                                    });
                                    this.closeInfoModal();
                                    this.props.setButtonStatus2('addEditPage')
                                });
                            });
                            
                        }
                    });
                }
                else {
                    this.props.updateState({
                        modifyData: res
                    }, () => {
                        this.props.processEditPageTemplate(() => {
                            this.props.updateState({
                                page: 'add-new',
                            });
                            this.closeInfoModal();
                            this.props.setButtonStatus2('addEditPage')
                        });
                    });
                    
                }
                
            }
        }
        catch(e) {
        }
    }

    async verifyOnlyInfo() {
        if(!this.props.form.isCheckNow(onlyInfoTemplate)) {
            return;
        }
        let values = this.props.form.getAllFormValue(onlyInfoTemplate);
        let postData = {
            pk_org: this.props.humanResourceOrganizationValue.refpk,
            model: values
        };

        try {
            let res = await verifyOnlyInfo(postData, 1);

            if(res.success) {

                if(res.data.confirmMsg) {
                    promptBox({
                        color: 'warning',
                        title: '',
                        content: (
                            <div 
                                dangerouslySetInnerHTML={{
                                    __html: res.data.confirmMsg
                                }}
                            ></div>
                        ),
                        beSureBtnClick: () => {
                            this.verifyOnlyInfoStep2();
                        }
                    });
                }
                else {
                    this.verifyOnlyInfoStep2();
                }
            }
        }
        catch(e) {
        }
    }

    closeInfoModal() {
        this.props.updateState({
            onlyInfoModalVisible: false
        });
        this.props.form.EmptyAllFormValue(onlyInfoTemplate);
    }

    showHandle() {
        this.getTemplate();
    }

    render() {
        const {
            onlyInfoModalVisible,
            form
        } = this.props;

        return (
            <NCModal
                show={onlyInfoModalVisible}
                size="sm"
                className="only-info-modal"
                onHide={this.closeInfoModal}
                onEntered={this.showHandle}
                backdrop="static"
            >
                <NCModalHeader
                    closeButton={true}
                >
                    {language['hi6007-000048']}{/* 国际化处理： 请输入人员唯一性信息*/}
                </NCModalHeader>
                <NCModalBody>
                    <div className="only-info-content">
                        {form.createForm(onlyInfoTemplate)}
                    </div>
                </NCModalBody>
                <NCModalFooter>
                    <NCButton
                        colors="primary"
                        onClick={this.verifyOnlyInfo}
                    >
                        {language['hi6007-000174']}{/* 国际化处理： 确认*/}
                    </NCButton>
                    <NCButton
                        colors="secondary"
                        onClick={this.closeInfoModal}
                    >
                        {language['hi6007-000005']}{/* 国际化处理： 取消*/}
                    </NCButton>
                </NCModalFooter>
            </NCModal>
        );
    }
}

export default OnlyInfoModal
